package controller.dictionary;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.MajorTypeServices;
import validator.NotBlankFluentValidator;
import validator.NotEmptyFluentValidator;
import wt.entity.dictionary.MajorType;
import wt.entity.dictionary.MajorTypeExample;
import wt.webPlugin.ZTree;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("dictionary/majorType")
public class MajorTypeController extends BaseController {

    @Autowired
    MajorTypeServices majorTypeServices;

    @RequiresPermissions("dictionary:majorType:default")
    @RequestMapping("index")
    public String index(Map map) throws Exception {
        MajorTypeExample majorTypeExample = new MajorTypeExample();
        majorTypeExample.createCriteria().andPidIsNull();
        map.put("itemMajor", majorTypeServices.selectByExample(majorTypeExample));

        return "dictionary/majorType/index";
    }

    @RequiresPermissions("dictionary:majorType:default")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "id", required = false) String id, @RequestParam(value = "pid", required = false) String pid, Map map) throws Exception {
        if (id != null) {
            MajorType majorType;
            majorType = majorTypeServices.selectByPrimaryKey(id);
            if (majorType == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, majorType);
            map.put(StaticConstant.EDIT_URL, "dictionary/majorType/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "dictionary/majorType/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        map.put("pid", pid);

        return "dictionary/majorType/editDlg";
    }


    @RequiresPermissions("dictionary:majorType:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult add(MajorType majorType) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(majorType.getName(), new NotBlankFluentValidator("名称"))
                .on(majorType.getPid(), new NotEmptyFluentValidator("父节点"))
                .on(majorType.getNum(), new NotBlankFluentValidator("编号"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }


        MajorType fatherMajorType = majorTypeServices.selectByPrimaryKey(majorType.getPid());
        if (0 != majorType.getNum().indexOf(fatherMajorType.getNum()) || majorType.getNum().length() == fatherMajorType.getNum().length()) {
            commonResult.addMessage("编号不符合编码规范");
            return commonResult.setError();
        }
        if (fatherMajorType.getCurrentDepth() >= fatherMajorType.getMaxDepth()) {
            commonResult.addMessage("专业深度超出范围");
            return commonResult.setError();
        }
        majorType.setCurrentDepth(fatherMajorType.getCurrentDepth() + 1);
        majorType.setMaxDepth(fatherMajorType.getMaxDepth());
        majorType.setLive(fatherMajorType.getLive());
        majorType.setItemId(fatherMajorType.getItemId());
        majorType.setOrders(new Date().getTime());

        majorTypeServices.insertSelective(majorType);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("dictionary:majorType:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(MajorType majorType) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(majorType.getName(), new NotBlankFluentValidator("名称"))
                .on(majorType.getNum(), new NotBlankFluentValidator("编号"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        majorType.setPid(null);
        majorTypeServices.updateByPrimaryKeySelective(majorType);
        return commonResult.setSuccess();
    }


    @RequiresPermissions("dictionary:majorType:default")
    @ResponseBody
    @RequestMapping("updateCheck")
    public CommonResult updateCheck(@RequestParam(value = "checkNodes[]", required = false) List<String> checkNodeList, @RequestParam(value = "noCheckNodes[]", required = false) List<String> noCheckNodeList) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        majorTypeServices.updateMajorTypeLive(checkNodeList, noCheckNodeList);
        return commonResult.setSuccess();
    }


    @RequiresPermissions("dictionary:majorType:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }
        majorTypeServices.deleteByPrimaryKey(id);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("dictionary:majorType:default")
    @ResponseBody
    @RequestMapping("select")
    public JSONArray select() throws Exception {
        List<MajorType> majorTypeList;
        majorTypeList = majorTypeServices.selectMajorType(null, false);
        JSONArray jsonArray = new JSONArray();
        for (MajorType majorType : majorTypeList) {
            JSONObject jsonObject;
            if (majorType.getLive() == 1) {
                jsonObject = ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getNum() + " " + majorType.getName(), true);
            } else {
                jsonObject = ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getNum() + " " + majorType.getName());
            }
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

}
